package com.sttk.fastMark.camera.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sttk.fastMark.camera.R;
import com.sttk.fastMark.camera.adapter.TemplateAdapter;
import com.sttk.fastMark.camera.basic.dialog.BaseSmartDialog;
import com.sttk.fastMark.camera.bean.TemplateBean;
import com.sttk.fastMark.camera.databinding.DialogSelTemplateBinding;

import java.util.ArrayList;
import java.util.List;

public class SelTemplateDialog extends BaseSmartDialog<DialogSelTemplateBinding> implements View.OnClickListener {

    private int selPosition;
    private TemplateAdapter templateAdapter;

    public SelTemplateDialog(@NonNull Context context) {
        super(context);
    }

    public void setSelPosition(int selPosition) {
        this.selPosition = selPosition;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_sel_template;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivCancel.setOnClickListener(this);

        mDataBinding.rvTemplateList.setLayoutManager(new GridLayoutManager(getContext(), 2));
        templateAdapter = new TemplateAdapter();
        mDataBinding.rvTemplateList.setAdapter(templateAdapter);
        templateAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                templateAdapter.getItem(selPosition).setSelected(false);
                selPosition = position;
                templateAdapter.getItem(position).setSelected(true);
                templateAdapter.notifyDataSetChanged();
                if (listener != null) {
                    listener.clickItem(position);
                }
            }
        });
        List<TemplateBean> list = new ArrayList<>();
        list.add(new TemplateBean(R.drawable.amb1, false));
        list.add(new TemplateBean(R.drawable.amb2, false));
        list.add(new TemplateBean(R.drawable.amb3, false));
        list.add(new TemplateBean(R.drawable.amb4, false));
        list.get(selPosition).setSelected(true);
        templateAdapter.setList(list);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ivCancel) {
            dismiss();
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickItem(int pos);
    }
}
